package com.majie.jedis;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * jedis连接池
 *
 * @author majie
 */
@Configuration
@ConditionalOnClass(JedisPool.class)
@EnableConfigurationProperties(JedisProperties.class)
public class JedisPoolAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public JedisPool JedisPool(JedisProperties jedisProperties) {

        JedisPoolConfig poolConfig = new JedisPoolConfig();

        poolConfig.setMaxIdle(jedisProperties.getPoolMaxIdle());

        poolConfig.setMaxTotal(jedisProperties.getPoolMaxTotal());

        poolConfig.setMinIdle(jedisProperties.getPoolMinIdle());

        poolConfig.setMaxWaitMillis(jedisProperties.getPoolMaxWait() * 1000);

        return new JedisPool(poolConfig, jedisProperties.getHost(), jedisProperties.getPort(),
                jedisProperties.getTimeout() * 1000, jedisProperties.getPassword(), jedisProperties.getDatabase());
    }

    @Bean
    @ConditionalOnMissingBean
    public JedisService jedisService( ) {
        return new JedisService();
    }
}